﻿/*
* jQuery插件
*
* 任意元素扩展为单选上传按钮
*
* By hongfei
*/

(function () {

    var defaults = {
        url: '',
        success: null,
        error: null,
        loadBegin: null,
        loadEnd: null,
        mouseOver: null,
        mouseOut: null
    };

    function uploadWrapper($wrapper, options) {
        this.$wrapper = $wrapper;
        this.url = options.url;
        this.success = $.delegate(options.success);
        this.error = $.delegate(options.error);
        this.loadBegin = $.delegate(options.loadBegin);
        this.loadEnd = $.delegate(options.loadEnd);
        this.mouseOver = $.delegate(options.mouseOver);
        this.mouseOut = $.delegate(options.mouseOut);
        this.initPos = {};
        this._init();
    }

    uploadWrapper.prototype = {
        constructor: uploadWrapper,
        _init: function () {
            this.$wrapper.removeClass('ui_upload_wrap').addClass('ui_upload_wrap');
            this.$image = this.$wrapper.find('img');
            if (this.$wrapper.find('.ui_uploader_s_file').length == 0) {
                this.$input = $('<input name="file" id="ui_uploader_s_file" type="file" hidefocus="true"/>');
                this.$wrapper.append(this.$input);
            }
            return this._bindEvents();
        },
        _bindEvents: function () {
            this.$wrapper.unbind()
                .mouseover(this._fileFollowBegin.delegate(this))
                .mousemove(this._fileFollow.delegate(this))
                .mouseout(this._fileFollowEnd.delegate(this));
            this.$input = this.$wrapper.find('input').unbind().change(this._uploadImage.delegate(this));
            if (this.$btnCancel) {
                this.$btnCancel.click(_cancelUpload);
            }
            return this;
        },
        /// 开始input:file跟随鼠标
        _fileFollowBegin: function (e) {
            this.initPos = this.$wrapper.offset();
            this._fileFollow(e);

            this.mouseOver();
        },
        /// 使input:file跟随鼠标
        _fileFollow: function (e) {
            this.initPos && this.$input.css({
                left: e.pageX - this.initPos.left - 40,
                top: e.pageY - this.initPos.top - 15
            });
        },
        /// 结束input:file跟随鼠标
        _fileFollowEnd: function (e) {
            this.$input.css({
                left: 0,
                top: 0
            });

            this.mouseOut();
        },
        _uploadImage: function () {
            if (this.$input.val().trim() == '') {
                return;
            }
            var data = {
                input: this.$input,
                url: this.url,
                dataType: 'json',
                success: this._uploadImageSuccess.delegate(this)
            };
            this.loadBegin();
            $.ajaxFile(data);
        },
        _uploadImageSuccess: function (result) {
            this.loadEnd();
            if (result.done) {
                // 成功时，调用成功回调函数
                this.success(result);
            } else {
                // 失败时，调用失败回调函数
                if (this.error(result) === false) {
                } else {
                    $.warning(result.msg, '提示');
                }
            }
            this._bindEvents();
        },
        _cancelUpload: function () {
        	$.ajaxFile.cancel();
            this.$input = this.$wrapper.find('input').unbind().change(this._uploadImage.delegate(this));
        }
    };

    $.fn.extend({
        uploadWrapper: function (params) {
            var options = $.mergeObject(defaults, params);
            this.each(function (i, item) {
                new uploadWrapper($(item), options);
            });
        }
    });

})();